package sys.bi.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import sys.bi.page.model.Cost;
import sys.bi.page.model.DataGrid;
import sys.bi.page.model.Json;
import sys.bi.service.CostService;
import sys.market.action.BaseAction;

import com.opensymphony.xwork2.ModelDriven;
@ParentPackage("default")
@Namespace("/bi/cost")
@Action(value="costAction")
@Results(value={
		@Result(name="list",location="/bi/cost/show.jsp"),
		@Result(name="selectDepartment",location="/bi/cost/selectDepartment.jsp"),
		@Result(name="ksjx",location="/bi/cost/ksjx.jsp")
	})
public class CostAction extends BaseAction  implements ModelDriven<Cost>{
	Cost cost = new Cost();
	CostService costService;
	public Cost getCost() {
		return cost;
	}
	public void setCost(Cost cost) {
		this.cost = cost;
	}
	public CostService getCostService() {
		return costService;
	}
	@Autowired
	public void setCostService(CostService costService) {
		this.costService = costService;
	}
	@Override
	public Cost getModel() {
		// TODO Auto-generated method stub
		return cost;
	}
	public String list() {
		DataGrid grid;
		try {
			grid = costService.list(cost);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		super.setAttribute("costList", grid);
		super.setAttribute("cost", cost);
		return "list";
	}
	public String selectDepartment() {
		DataGrid grid;
		try {
			grid = costService.selectDepartment(cost);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		super.setAttribute("costList", grid);
		super.setAttribute("cost", cost);
		return "selectDepartment";
	}
	public void add() {
		Json json = new Json();
		try {
	    	costService.add(cost);
		}catch(Exception ex) {
			ex.printStackTrace();
			json.setSuccess(false);
			json.setMsg(ex.getMessage());
		}
		json.setSuccess(true);
		json.setMsg("保存成功!");
		super.writeJson(json);
	}
	public String ksjx() {
		DataGrid grid;
		try {
			grid = costService.ksjx(cost);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		super.setAttribute("prizeList", grid);
		super.setAttribute("cost", cost);
		return "ksjx";
	}
}
